
package panels;

import core.COINCOMOComponent;
import core.COINCOMOSubComponent;
import core.COINCOMOSubSystem;
import core.COINCOMOSystem;
import core.COINCOMOUnit;
import database.COINCOMOComponentManager;
import database.COINCOMOSubComponentManager;
import database.COINCOMOSubSystemManager;
import database.COINCOMOSystemManager;
import dialogs.ComponentScaleFactorsDialog;
import dialogs.ComponentScheduleDriverDialog;
import dialogs.SubComponentEAFDialog;
import dialogs.SubComponentSizeDialog;
import dialogs.SubSystemEAFDialog;
import dialogs.SubSystemSizeDialog;
import extensions.COINCOMOCheckBoxCellEditor;
import extensions.COINCOMOCheckBoxTableCellRenderer;
import extensions.COINCOMOClefTableHeaderRenderer;
import extensions.COINCOMOFixedTable;
import extensions.COINCOMOClefTableCellRenderer;
import extensions.COINCOMOComboBoxCellEditor;
import extensions.COINCOMOVector;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import java.util.Enumeration;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import main.COINCOMO;
import main.GlobalMethods;
import main.Icons;

/**
 *
 * @author Raed Shomali
 */
public class CosysmoverviewPanelSS extends JPanel implements ActionListener , ItemListener , MouseListener
{
    // Component Being Analyzed ...
    public static COINCOMOComponent component = null;

    // System Being Analyzed ...
    public static COINCOMOSystem system = null;

    public static COINCOMOSubSystem subSystem = null;
    // Buttons
 //  JButton addSubSystemButton = new JButton( "Run" );

  /*   JButton deleteSubSystemButton = new JButton( "Subsystem(s)" );


    // Combo Box
    JComboBox comboBox = new JComboBox(); */
    // Tables
    public static DefaultTableModel clefTableModel = new DefaultTableModel();

    public static COINCOMOFixedTable clefTable = new COINCOMOFixedTable( clefTableModel );

    public static JTextPane estimationTextPane = new JTextPane();

    JScrollPane estimationScroller = new JScrollPane( estimationTextPane );

    JSplitPane splitPane = new JSplitPane();

    static DecimalFormat format1Decimal = new DecimalFormat( "0.0" );

    static DecimalFormat format2Decimals = new DecimalFormat( "0.00" );

    static UpdateEstimationReport reportUpdater = null;
    static RefreshSubSystems refreshThread = null ;

    static boolean isLoading = true ;

    /**
     *
     * @param component is used to view its content on the panel
     */
    public CosysmoverviewPanelSS(COINCOMOSubSystem subSystem)
    {
        CosysmoverviewPanelSS.subSystem = subSystem;

        // ComboBox
     /*   comboBox.addItemListener( this );

        comboBox.setPreferredSize( new Dimension( 125 , 25 ) );

        comboBox.setBackground( Color.WHITE );
        comboBox.setRenderer( new COINCOMOComboBoxCellEditor() );

        comboBox.addItem( " More Actions ..." );
        comboBox.addItem( "SEPARATOR" );
        comboBox.addItem( " Select All" );
        comboBox.addItem( " Toggle" );
        comboBox.addItem( " Unselect All" );

        // Buttons
        //scaleFactorsButton = new JButton( "Scale Factors" );
        //scaleFactorsButton.setText( "Scale Factors " + component.getScaleFactor() );

        addSubSystemButton.setIcon( Icons.ADD_SUBSYSTEM_ICON );
        deleteSubSystemButton.setIcon( Icons.DELETE_SUBSYSTEM_ICON );

        addSubSystemButton.addActionListener( this );
        deleteSubSystemButton.addActionListener( this );
        //scaleFactorsButton.addActionListener( this );
        //scheduleButton.addActionListener( this );

        addSubSystemButton.setFocusable( false );
        deleteSubSystemButton.setFocusable( false );
        //scaleFactorsButton.setFocusable( false );
        //scheduleButton.setFocusable( false );

        addSubSystemButton.setPreferredSize( new Dimension( 150 , 25 ) );
        deleteSubSystemButton.setPreferredSize( new Dimension( 160 , 25 ) );
        //scaleFactorsButton.setPreferredSize( new Dimension( 160 , 25 ) );
        //scheduleButton.setPreferredSize( new Dimension( 100 , 25 ) ); */

        // Split Pane


  /*      addSubSystemButton.setIcon( Icons.SAVE_ICON );
        addSubSystemButton.addActionListener( this );
        addSubSystemButton.setFocusable( false );
        addSubSystemButton.setPreferredSize( new Dimension( 150 , 25 ) ); */

        splitPane.setOrientation( JSplitPane.VERTICAL_SPLIT );
        splitPane.setContinuousLayout( true );
        splitPane.setDividerLocation( 240 );

        // Text Pane
        TitledBorder dictionaryTitleBorder = BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder( EtchedBorder.RAISED ) , "Results" );
        dictionaryTitleBorder.setTitleColor( Color.BLUE );
        dictionaryTitleBorder.setTitlePosition( TitledBorder.BELOW_TOP );
        dictionaryTitleBorder.setTitleJustification( TitledBorder.CENTER );
        estimationTextPane.setEditable( false );
        estimationScroller.setBorder( dictionaryTitleBorder );
        estimationTextPane.setContentType( "text/html" );
        estimationTextPane.setMargin( new Insets( 10 , 10 , 10 , 10 ) );

        // To Avoid Unnecessary Refreshes
        isLoading = true ;

        updateEstimationTextPane( false );

        isLoading = false ;

        // Clear
        clefTableModel = new DefaultTableModel();
        clefTable = new COINCOMOFixedTable( clefTableModel );

        // Table
        clefTable.setRowSelectionAllowed( false );
        clefTable.addMouseListener( this );

        // Html Was added to be able to make it Multiline ..
        clefTableModel.addColumn( "X" );
        clefTableModel.addColumn( "Name" );
        clefTableModel.addColumn( "System Size" );
        clefTableModel.addColumn( "<html><body style='text-align:center'>System Labor<br />Rates<br /></body></html>" );
        clefTableModel.addColumn( "System Cost Drivers" );

        COINCOMOClefTableHeaderRenderer multiLineTableHeaderRenderer = new COINCOMOClefTableHeaderRenderer();
        COINCOMOClefTableCellRenderer colorfulTableCellRenderer = new COINCOMOClefTableCellRenderer();

        Enumeration<TableColumn> columns = clefTable.getColumnModel().getColumns();

        // Go Through All Columns ..
        while ( columns.hasMoreElements() )
        {
            // Set Each with Our Table Header Renderer ...
            TableColumn column = (TableColumn) columns.nextElement();
            column.setHeaderRenderer( multiLineTableHeaderRenderer );
            column.setCellRenderer( colorfulTableCellRenderer );
        }

        // Setting the Width of Some Columns ...
        clefTable.getColumnModel().getColumn( 0 ).setPreferredWidth( 15 );
        clefTable.getColumnModel().getColumn( 4 ).setPreferredWidth( 50 );
        //clefTable.getColumnModel().getColumn( 6 ).setPreferredWidth( 50 );
        //clefTable.getColumnModel().getColumn( 7 ).setPreferredWidth( 50 );
        //clefTable.getColumnModel().getColumn( 8 ).setPreferredWidth( 50 );
        /*clefTable.getColumnModel().getColumn( 9 ).setPreferredWidth( 50 );
        clefTable.getColumnModel().getColumn( 10 ).setPreferredWidth( 50 );
        clefTable.getColumnModel().getColumn( 11 ).setPreferredWidth( 50 );
        clefTable.getColumnModel().getColumn( 12 ).setPreferredWidth( 50 );*/

        clefTable.getColumnModel().getColumn( 0 ).setCellRenderer( new COINCOMOCheckBoxTableCellRenderer() );
        clefTable.getColumnModel().getColumn( 0 ).setCellEditor( new COINCOMOCheckBoxCellEditor() );

        // Design Gui of Layouts
        FlowLayout rightFlow = new FlowLayout();
        FlowLayout leftFlow = new FlowLayout();

        rightFlow.setAlignment( FlowLayout.RIGHT );
        leftFlow.setAlignment( FlowLayout.LEFT );

        // First of Two Halves of the North Panel
        JPanel upperNorthPanel = new JPanel( rightFlow );
        //upperNorthPanel.add( scaleFactorsButton );
        //upperNorthPanel.add( scheduleButton );

        // Second of two Halves of the North Panel
        JPanel lowerNorthPanel = new JPanel( leftFlow );
        lowerNorthPanel.add( new JLabel( "  " ) );
     /*   lowerNorthPanel.add( addSubSystemButton );
        lowerNorthPanel.add( deleteSubSystemButton );
        lowerNorthPanel.add( comboBox );*/


//        lowerNorthPanel.add( addSubSystemButton );
        JPanel northPanel = new JPanel( new GridLayout( 2 , 1 ) );
        northPanel.add( upperNorthPanel );
        northPanel.add( lowerNorthPanel );

        JPanel southPanel = new JPanel( new BorderLayout() );
        southPanel.add( estimationScroller );
        southPanel.add( new JLabel( "    " ) , BorderLayout.NORTH );
        southPanel.add( new JLabel( "    " ) , BorderLayout.SOUTH );
        southPanel.add( new JLabel( "    " ) , BorderLayout.EAST );
        southPanel.add( new JLabel( "    " ) , BorderLayout.WEST );

        JPanel centerPanel = new JPanel( new BorderLayout() );
        centerPanel.add( new JScrollPane( clefTable ) );
        centerPanel.add( new JLabel( "    " ) , BorderLayout.NORTH );
        centerPanel.add( new JLabel( "    " ) , BorderLayout.SOUTH );
        centerPanel.add( new JLabel( "    " ) , BorderLayout.EAST );
        centerPanel.add( new JLabel( "    " ) , BorderLayout.WEST );

        // Load the Sub Components ...
        loadSubSystems();

        splitPane.setTopComponent( centerPanel );
        splitPane.setBottomComponent( southPanel );

        this.setLayout( new BorderLayout() );
        this.add( northPanel , BorderLayout.NORTH );
        this.add( splitPane );
    }

    public static void updateEstimationTextPane( final boolean needToUpdateComponent )
    {
        estimationTextPane.setText( "Loading ..." );

        if( !isLoading )
        {
            // If Already Exists ..
            if( refreshThread != null )
            {
                // Kill it
                refreshThread.interrupt() ;
            }

            refreshThread = new RefreshSubSystems() ;
            refreshThread.start();

            // Already Exists One
            if ( reportUpdater != null )
            {
                // Kill It
                reportUpdater.interrupt();
            }
        }

        // Create a New One ...
        reportUpdater = new UpdateEstimationReport( needToUpdateComponent );
        reportUpdater.start();
    }

    private void loadSubSystems()
    {
        //Vector<COINCOMOUnit> orderedVector = GlobalMethods.getOrderedVector( system.getListOfSubUnits() );

        for ( int i = 0 ; i < 1 ; i++ )
        {
            COINCOMOSubSystem tempSubSystem = subSystem;

            // Create a Row ...
            COINCOMOVector<String> tableRowVector = new COINCOMOVector<String>();

            tableRowVector.add( "false" );
            tableRowVector.add( tempSubSystem.getName() );
            tableRowVector.add( tempSubSystem.getFinalSLOC() + "" );
            tableRowVector.add( format1Decimal.format( GlobalMethods.roundOff( tempSubSystem.getLaborRate() , 1 ) ) );
            tableRowVector.add( format2Decimals.format( GlobalMethods.roundOff( tempSubSystem.getEaf() , 2 ) ) );
            tableRowVector.add( tempSubSystem.getLanguage() );
            tableRowVector.add( format2Decimals.format( GlobalMethods.roundOff( tempSubSystem.getNomEffort() , 2 ) ) );
            tableRowVector.add( format2Decimals.format( GlobalMethods.roundOff( tempSubSystem.getEstEffort() , 2 ) ) );
            tableRowVector.add( format2Decimals.format( GlobalMethods.roundOff( tempSubSystem.getProd() , 2 ) ) );
            tableRowVector.add( format2Decimals.format( GlobalMethods.roundOff( tempSubSystem.getCost() , 2 ) ) );
            tableRowVector.add( format2Decimals.format( GlobalMethods.roundOff( tempSubSystem.getInstCost() , 2 ) ) );
            tableRowVector.add( format1Decimal.format( GlobalMethods.roundOff( tempSubSystem.getStaff() , 1 ) ) );
            tableRowVector.add( "0.0" );

            // Set Same ID
            tableRowVector.setRowID( tempSubSystem.getUnitID() );

            // Add a New Row To Table ...
            clefTableModel.addRow( tableRowVector );
        }

        GlobalMethods.updateStatusBar( "SubSystem has been loaded." );
    }
//Written by Pradeep..............................................
    public void actionPerformed( ActionEvent e )
    {
      /*  if ( e.getSource() == addSubSystemButton )
        {
            addSubSystemButton.setEnabled( false );

            SwingUtilities.invokeLater( new Runnable()
            {
                public void run()
                {
                    //Arnab
                    // COINCOMOSubSystemManager.calculateCosysmoCostDriver(subSystem);
                   
                }

            } );
        } */

        /*
        else if ( e.getSource() == deleteSubSystemButton )
        {
            // Get Selected Row's Index
            final Vector<Integer> selectedRowsIndexes = new Vector<Integer>();

            for ( int i = 0 ; i < clefTable.getRowCount() ; i++ )
            {
                // If Selected ...
                if ( clefTable.getValueAt( i , 0 ).equals( "true" ) )
                {
                    // Add to the list of rows
                    selectedRowsIndexes.add( i );
                }
            }

            // No Row Selected
            if ( selectedRowsIndexes.isEmpty() )
            {
                GlobalMethods.updateStatusBar( "No Subsystems were Selected." , Color.RED );

                return;
            }

            // Contains the Sub components deletion message ..
            JTextArea textArea = new JTextArea( 8 , 40 );

            textArea.setLineWrap( true );
            textArea.setWrapStyleWord( true );
            textArea.setMargin( new Insets( 10 , 10 , 10 , 10 ) );
            textArea.setFont( new Font( "courier" , 0 , 12 ) );

            textArea.append( "Are you sure you would like to delete the following subsystems ? \n\n" );

            // Print Out Selected Sub components for deletion ..
            for ( int i = 0 ; i < selectedRowsIndexes.size() ; i++ )
            {
                textArea.append( "\t" + (i + 1) + ") " + clefTable.getValueAt( Integer.parseInt( selectedRowsIndexes.get( i ) + "" ) , 1 ) + "\n" );
            }

            // Put cursor at the beginning ...
            textArea.setCaretPosition( 0 );

            // Confirm Deletion ...
            int confirmationAnswer = JOptionPane.showConfirmDialog( COINCOMO.application , new JScrollPane( textArea ) , "Confirm Deletion" , JOptionPane.YES_NO_OPTION );

            // Delete Only if Yes is Chosen ...
            if ( confirmationAnswer == JOptionPane.YES_OPTION )
            {
                deleteSubSystemButton.setEnabled( false );

                SwingUtilities.invokeLater( new Runnable()
                {
                    public void run()
                    {
                        Vector<COINCOMOSubSystem> subSystemsToBeDeleted = new Vector<COINCOMOSubSystem>();

                        // Delete Rows ...
                        for ( int i = 0 ; i < selectedRowsIndexes.size() ; i++ )
                        {
                            // get the Vector of Vectors ..
                            Vector<COINCOMOVector> vectorOfRows = (Vector<COINCOMOVector>) clefTableModel.getDataVector();

                            // Get Row Vector
                            COINCOMOVector selectedRowVector = (COINCOMOVector) vectorOfRows.get( Integer.parseInt( selectedRowsIndexes.get( i ) + "" ) - i );

                            // Remove Internally ..
                            subSystemsToBeDeleted.add( (COINCOMOSubSystem) system.getListOfSubUnits().remove( selectedRowVector.getRowID() ) );

                            // Remove From Table
                            clefTableModel.removeRow( Integer.parseInt( selectedRowsIndexes.get( i ) + "" ) - i );
                        }

                        // Remove From Database
                        COINCOMOSubSystemManager.deleteSubComponents(subSystemsToBeDeleted );

                        deleteSubSystemButton.setEnabled( true );

                        // Update Report
                        updateEstimationTextPane( true );

                        GlobalMethods.updateStatusBar( selectedRowsIndexes.size() + " Subsystems were deleted." );
                    }

                } );
            }
        }

        else
        {
           // new ComponentScheduleDriverDialog( COINCOMO.application , system );
        }*/
    } 

    public void itemStateChanged( ItemEvent e )
    {
        // Only Check Items When Selected ...
        if ( e.getStateChange() == ItemEvent.SELECTED )
        {
            // Select All
      /*      if ( comboBox.getSelectedIndex() == 2 )
            {
                for ( int i = 0 ; i < clefTable.getRowCount() ; i++ )
                {
                    clefTable.setValueAt( "true" , i , 0 );
                }
            }
            // Toggle
            else if ( comboBox.getSelectedIndex() == 3 )
            {
                for ( int i = 0 ; i < clefTable.getRowCount() ; i++ )
                {
                    clefTable.setValueAt( !clefTable.getValueAt( i , 0 ).equals( "true" ) + "" , i , 0 );
                }
            }
            // Unselect All
            else if ( comboBox.getSelectedIndex() == 4 )
            {
                for ( int i = 0 ; i < clefTable.getRowCount() ; i++ )
                {
                    clefTable.setValueAt( "false" , i , 0 );
                }
            }

            // Go Back To Default ... "More Actions .."
            comboBox.setSelectedIndex( 0 ); */
        }
    }

    public void mouseClicked( MouseEvent e )
    {
        // translate point to row and column numbers
        final int rowNumber = clefTable.rowAtPoint( e.getPoint() );
        final int columnNumber = clefTable.columnAtPoint( e.getPoint() );

        Vector<COINCOMOVector> vectors = clefTableModel.getDataVector();

        COINCOMOVector clickedRow = vectors.get( rowNumber );

      //  final COINCOMOSubSystem subSystem = (COINCOMOSubSystem) subSystem;

        if ( columnNumber == 1 )
        {
            String result = JOptionPane.showInputDialog( COINCOMO.application , "Please enter the new name:" );

            // If Not Empty
            if ( result != null && !result.trim().equals( "" ) )
            {
                // Update Name of the SubComponent
                subSystem.setName( result );

                GlobalMethods.updateStatusBar( "Saving New Name..." );

                // Update Name in the Table
                clefTable.setValueAt( subSystem.getName() , rowNumber , columnNumber );

                // Create a Thread
                SwingUtilities.invokeLater( new Runnable()
                {
                    public void run()
                    {
                        try
                        {
                            // Rest a Bit .. to allow GUI to render
                            Thread.sleep( 100 );

                            // Save in Database ...
                            COINCOMOSubSystemManager.updateSubSystem( subSystem );

                            GlobalMethods.updateStatusBar( "New name has been saved." );
                        }
                        catch ( InterruptedException ex )
                        {
                        }
                    }

                } );
            }
        }
        else if ( columnNumber == 2 )
        {
            new SubSystemSizeDialog( COINCOMO.application , subSystem , rowNumber );
        }
        else if ( columnNumber == 3 )
        {
            String result = JOptionPane.showInputDialog( COINCOMO.application , "Please enter the new labor rate:" );

            // If Not Empty
            if ( result != null && !result.trim().equals( "" ) )
            {
                if ( GlobalMethods.isNonNegativeFloat( result ) )
                {
                    // Update Labor Rate of the SubComponent
                    subSystem.setLaborRate( Float.parseFloat( result ) );

                    GlobalMethods.updateStatusBar( "Saving New Labor Rate..." );

                    estimationTextPane.setText( "Loading ..." );

                    clefTable.setValueAt( format1Decimal.format( GlobalMethods.roundOff( subSystem.getLaborRate() , 1 ) ) , rowNumber , columnNumber );

                    // Create a Thread
                    SwingUtilities.invokeLater( new Runnable()
                    {
                        public void run()
                        {
                            try
                            {
                                // Rest a Bit to allow GUI to render ...
                                Thread.sleep( 100 );

                                // Save in Database ...
                                COINCOMOSubSystemManager.updateSubSystem( subSystem );
                                COINCOMOSystemManager.updateSystem( system );

                                // Update Table ...
                                clefTable.setValueAt( format2Decimals.format( GlobalMethods.roundOff( subSystem.getEaf() , 2 ) ) , rowNumber , 4 );
                               /* clefTable.setValueAt( format2Decimals.format( GlobalMethods.roundOff( subSystem.getNomEffort() , 2 ) ) , rowNumber , 6 );
                                clefTable.setValueAt( format2Decimals.format( GlobalMethods.roundOff( subSystem.getEstEffort() , 2 ) ) , rowNumber , 7 );
                                clefTable.setValueAt( format2Decimals.format( GlobalMethods.roundOff( subSystem.getProd() , 2 ) ) , rowNumber , 8 );
                                clefTable.setValueAt( format2Decimals.format( GlobalMethods.roundOff( subSystem.getCost() , 2 ) ) , rowNumber , 9 );
                                clefTable.setValueAt( format2Decimals.format( GlobalMethods.roundOff( subSystem.getInstCost() , 2 ) ) , rowNumber , 10 );
                                clefTable.setValueAt( format1Decimal.format( GlobalMethods.roundOff( subSystem.getStaff() , 1 ) ) , rowNumber , 11 );*/

                                // Update ...
                                updateEstimationTextPane( false );

                                GlobalMethods.updateStatusBar( "New Labor Rate has been saved." );
                            }
                            catch ( InterruptedException ex )
                            {
                                Logger.getLogger( ComponentOverviewPanel.class.getName() ).log( Level.SEVERE , null , ex );
                            }
                        }

                    } );
                }
                else
                {
                    GlobalMethods.updateStatusBar( "Labor Rate Must Be a Non-Negative Value." , Color.RED );
                }
            }
        }
        else if ( columnNumber == 4 )
        {
            new SubSystemEAFDialog( COINCOMO.application , subSystem , rowNumber );

        }
    }

    public void mousePressed( MouseEvent e )
    {
    }

    public void mouseReleased( MouseEvent e )
    {
    }

    public void mouseEntered( MouseEvent e )
    {
    }

    public void mouseExited( MouseEvent e )
    {
    }

    static class RefreshSubSystems extends Thread
    {
        public RefreshSubSystems()
        {
            this.setPriority( 3 );
        }

        @Override
        public void run()
        {
            GlobalMethods.updateStatusBar( "Refreshing SubSystems ..." );

            try
            {
                Vector<COINCOMOUnit> orderedVector = GlobalMethods.getOrderedVector( system.getListOfSubUnits() );

           //    COINCOMOSubSystemManager.updateSubSystem( orderedVector );

                for ( int i = 0 ; i < orderedVector.size() ; i++ )
                {
                    COINCOMOSubSystem tempSubComponent = (COINCOMOSubSystem) orderedVector.get( i );

                    clefTable.setValueAt( format2Decimals.format( GlobalMethods.roundOff( tempSubComponent.getNomEffort() , 2 ) ) , i , 6 );
                    clefTable.setValueAt( format2Decimals.format( GlobalMethods.roundOff( tempSubComponent.getEstEffort() , 2 ) ) , i , 7 );
                    clefTable.setValueAt( format2Decimals.format( GlobalMethods.roundOff( tempSubComponent.getProd() , 2 ) ) , i , 8 );
                    clefTable.setValueAt( format2Decimals.format( GlobalMethods.roundOff( tempSubComponent.getCost() , 2 ) ) , i , 9 );
                    clefTable.setValueAt( format2Decimals.format( GlobalMethods.roundOff( tempSubComponent.getInstCost() , 2 ) ) , i , 10 );
                    clefTable.setValueAt( format1Decimal.format( GlobalMethods.roundOff( tempSubComponent.getStaff() , 1 ) ) , i , 11 );
                }
            }
            catch( Exception e )
            {
            }

            GlobalMethods.updateStatusBar( "Subsystems have been refreshed." );
        }
    }

    static class UpdateEstimationReport extends Thread
    {
        boolean needToUpdateComponent;

        public UpdateEstimationReport( boolean needToUpdate )
        {
            this.needToUpdateComponent = needToUpdate;

            // Lowest Priority ...
            // So User can Play around with the System.
            this.setPriority( 1 );
        }

        @Override
        public void run()
        {
            GlobalMethods.updateStatusBar( "Updating Report ..." );


            if ( needToUpdateComponent )
            {
                // Update Database...
                COINCOMOSystemManager.updateSystem( system );
            }

           // OverviewsAndGraphsPanel.copsemo.updateCOPSEMO( component );

            StringBuilder output = new StringBuilder();

            output.append( "<table border = '0' cellpadding = '1'  cellspacing = '1' width = '50%' >" );
            output.append( "<tr>" );
            output.append( "<td> <b>Systems Engineering</b></td>" );
            output.append( "</tr>" );
            output.append("<br/>");
            output.append( "<tr>" );
            output.append( "<td>Effort = 0 Person-Months</td>" );
            output.append( "</tr>" );
            output.append( "<tr>" );
            output.append( "<td>Schedule = 0 Months</td>" );
            output.append( "</tr>" );
            output.append( "<tr>" );
            output.append( "<td>Cost = $0</td>" );
            output.append("<br/>");
            output.append( "</tr>" );
            output.append( "<tr>" );
            output.append( "<td> <b>Effort Distribution(Person-Months)</b></td>" );
           // output.append( "<td> <b>Hours/PM:</b> 152 </td>" );
            output.append( "</tr>" );
            output.append( "</table>" );

            String color = "DDDDDD";

            output.append( "<table border = '1' cellpadding = '1'  cellspacing = '1' width = '100%' align = 'center'>" );
            output.append( "<tr>" );
            output.append( "<th bgcolor = " + color + "> Phase/Activity </th>" );
            output.append( "<th bgcolor = " + color + "> Conceptualize</th>" );
            output.append( "<th bgcolor = " + color + "> Develop </th>" );
            output.append( "<th bgcolor = " + color + "> Operational Test and Evaluation </th>" );
            output.append( "<th bgcolor = " + color + "> Transition to Operation </th>" );
            output.append( "</tr>" );

            output.append("<tr>");
            output.append("<td>Acquistion and Supply</td>");
            output.append("<td>0.0</td>");
            output.append("<td>0.0</td>");
            output.append("<td>0.0</td>");
            output.append("<td>0.0</td>");
            output.append("</tr>");

              output.append("<tr>");
            output.append("<td>Technical Management</td>");
            output.append("<td>0.0</td>");
            output.append("<td>0.0</td>");
            output.append("<td>0.0</td>");
            output.append("<td>0.0</td>");
            output.append("</tr>");

              output.append("<tr>");
            output.append("<td>System Design</td>");
            output.append("<td>0.0</td>");
            output.append("<td>0.0</td>");
            output.append("<td>0.0</td>");
            output.append("<td>0.0</td>");
            output.append("</tr>");

              output.append("<tr>");
            output.append("<td>Product Realization</td>");
            output.append("<td>0.0</td>");
            output.append("<td>0.0</td>");
            output.append("<td>0.0</td>");
            output.append("<td>0.0</td>");
            output.append("</tr>");

            output.append("<tr>");
            output.append("<td>Product Evaluation</td>");
            output.append("<td>0.0</td>");
            output.append("<td>0.0</td>");
            output.append("<td>0.0</td>");
            output.append("<td>0.0</td>");
            output.append("</tr>");
           /* output.append( "<tr>" );
            output.append( "<th bgcolor = " + color + "> Most Likely </th>" );
            output.append( "<th> " + format2Decimals.format( GlobalMethods.roundOff( component.getTotalEffort() , 2 ) ) + " </th>" );
            output.append( "<th> " + format2Decimals.format( GlobalMethods.roundOff( component.getTotalSchedule() , 2 ) ) + " </th>" );
            output.append( "<th> " + format2Decimals.format( GlobalMethods.roundOff( component.getTotalProd() , 2 ) ) + " </th>" );
            output.append( "<th> " + format2Decimals.format( GlobalMethods.roundOff( component.getTotalCost() , 2 ) ) + " </th>" );
            output.append( "<th> " + format2Decimals.format( GlobalMethods.roundOff( component.getTotalInstCost() , 2 ) ) + " </th>" );
            output.append( "<th> " + format1Decimal.format( GlobalMethods.roundOff( component.getTotalStaff() , 1 ) ) + " </th>" );
            output.append( "<th> 0.0 </th>" );
            output.append( "</tr>" ); */
            output.append( "</table>" );

            estimationTextPane.setText( output.toString() );


            GlobalMethods.updateStatusBar( "Report Updated." );
        }

    }

}
